<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="lib/html5shiv.js"></script>
<script type="text/javascript" src="lib/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css" />
<link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css" />
<!--[if IE 6]>
<script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>用户管理</title>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 系统管理 <span class="c-gray en">&gt;</span> 用户管理 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container" id="app">
	<div class="text-c">
		<!-- 日期范围：
		<input type="text" onfocus="WdatePicker({ maxDate:'#F{$dp.$D(\'datemax\')||\'%y-%M-%d\'}' })" id="datemin" class="input-text Wdate" style="width:120px;">
		-
		<input type="text" onfocus="WdatePicker({ minDate:'#F{$dp.$D(\'datemin\')}',maxDate:'%y-%M-%d' })" id="datemax" class="input-text Wdate" style="width:120px;"> -->
		分公司: <span class="select-box inline">
			<select class="select" id="companyid" name="companyid" onchange="queryOffice(this.value)"></select>
		</span>
		票务室: <span class="select-box inline">
			<select class="select" id="officeid" name="officeid">
				<option value>请选择票务室</option>
			</select>
		</span>
		<!-- <br/>
		<span class="select-box inline">
			<select class="input-text" id="officeid" name="officeid"></select>
		</span> -->
		<input type="text" class="input-text" style="width:250px" placeholder="输入会员名称、电话、邮箱" id="" name="" v-model="msg">
		<span class="select-box inline">
			<select class="select" id="enabled">
				<option value="2">全部</option>
				<option value="1">启用</option>
				<option value="0">停用</option>
			</select>
		</span>
		<button type="submit" class="btn btn-success radius" id="" name="" v-on:click="query()"><i class="Hui-iconfont">&#xe665;</i> 搜用户</button>
	</div>
	<div class="cl pd-5 bg-1 bk-gray mt-20"> <span class="l"><a href="javascript:;" onclick="datadel()" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a> <a href="javascript:;" onclick="member_add('添加用户','authinfo-user-add.html','','510')" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加用户</a></span></div>
	<div class="mt-20">
	<table id="list" class="table table-border table-bordered table-hover table-bg table-sort">
	</table>
	</div>
</div>
<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="js/jquery.base64.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script> 
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/My97DatePicker/4.8/WdatePicker.js"></script> 
<script type="text/javascript" src="lib/datatables/1.10.0/jquery.dataTables.min.js"></script> 
<script type="text/javascript" src="lib/laypage/1.2/laypage.js"></script>
<script type="text/javascript" src="js/vue.js" charset="utf-8"></script>
<script type="text/javascript">
/*用户-添加*/
function member_add(title,url,w,h){
	layer_show(title,url,w,h);
}
/*用户-查看*/
function member_show(title,url,id,w,h){
	layer_show(title,url,w,h);
}
/*用户-停用*/
function member_stop(obj, id){
	layer.confirm('确认要停用吗？',function(index){
		$.ajax({
			type: 'POST',
			url: ipAddress + 'auth/user/enabledUser',
			data: {
				username: id,
				enabled: false
			},
			beforeSend: function (request) {
				request.setRequestHeader("Authorization", "Bearer " + getToken());
			},
			dataType: 'json',
			success: function(data){
				$(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="member_start(this,id)" href="javascript:;" title="启用"><i class="Hui-iconfont">&#xe6e1;</i></a>');
				$(obj).parents("tr").find(".td-status").html('<span class="label label-defaunt radius">已停用</span>');
				$(obj).remove();
				layer.msg('已停用!',{icon: 5,time:1000});
			},
			error:function(data) {
				console.log(data.msg);
			},
		});		
	});
}

/*用户-启用*/
function member_start(obj,id){
	layer.confirm('确认要启用吗？',function(index){
		$.ajax({
			type: 'PUT',
			// url: 'http://localhost:8080/auth/user/enabledUser',
			url: ipAddress + 'auth/user/enabledUser',
			data: {
				username: id,
				enabled: true
			},
			beforeSend: function (request) {
				request.setRequestHeader("Authorization", "Bearer " + getToken());
			},
			dataType: 'json',
			success: function(data){
				$(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="member_stop(this,id)" href="javascript:;" title="停用"><i class="Hui-iconfont">&#xe631;</i></a>');
				$(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已启用</span>');
				$(obj).remove();
				layer.msg('已启用!',{icon: 6,time:1000});
			},
			error:function(data) {
				console.log(data.msg);
			},
		});
	});
}
/*用户-编辑*/
function member_edit(title,url,id,w,h){
	layer_show(title,url + "?username=" + id,w,h);
}
/*密码-修改*/
function change_password(title,url,id,w,h){
	layer_show(title,url + "?username=" + id,w,h);	
}
/*用户-删除*/
function member_del(obj,id){
	layer.confirm('确认要删除吗？',function(index){
		$.ajax({
			type: 'DELETE',
			// url: 'http://localhost:8080/auth/user/deleteUser',
			url: ipAddress + 'auth/user/deleteUser',
			data: {
				username: id
			},
			beforeSend: function (request) {
				request.setRequestHeader("Authorization", "Bearer " + getToken());
			},
			dataType: 'json',
			success: function(data){
				$(obj).parents("tr").remove();
				layer.msg('已删除!',{icon:1,time:1000});
			},
			error:function(data) {
				console.log(data.msg);
			}
		});		
	});
}

/*用户-批量删除*/
function datadel(){
	var checkID = '';//定义一个空数组 
	var members = $("input[name='usernames']:checked");
	$(members).each(function(i){//把所有被选中的复选框的值存入数组
		checkID = checkID + $(this).val() + ',';
	});
	layer.confirm('确认要删除吗？',function(index){
		$.ajax({
			type: 'DELETE',
			// url: 'http://localhost:8080/auth/user/deleteUserByIds',
			url: ipAddress + 'auth/user/deleteUserByIds',
			data: {
				ids: checkID
			},
			beforeSend: function (request) {
				request.setRequestHeader("Authorization", "Bearer " + getToken());
			},
			dataType: 'json',
			success: function(data){
				$(members).each(function(i){
					$(this).parents("tr").remove();
				});
				layer.msg('已删除!',{icon:1,time:1000});
			},
			error:function(data) {
				console.log(data.msg);
			}
		});		
	});
}
</script> 
<script type="text/javascript">
	function queryOffice(companyid){
		$.ajax({
			url: ipAddress + 'base/office/getList',
			// url: 'http://localhost:8080/base/office/getList',
			data: {
				"company.companyid": companyid,
				order: "id"
			},
			beforeSend: function (request) {
				request.setRequestHeader("Authorization", "Bearer " + getToken());
			},
			method:'GET',
			success:function (data,status,request) {
				console.log(data);
				$("#officeid").empty();
				$("#officeid").append("<option value>请选择票务室</option>");
				for(var i = 0; i < data.length; i ++){
					$("#officeid").append("<option value='" + data[i].id + "'>" + data[i].officename + "</option>");
				}
			},
			error:function (request, msg, obj) {
				var respTxtObj = JSON.parse(request.responseText);
				console.log(respTxtObj);
				console.log(respTxtObj.error);
				console.log(respTxtObj.error_description);
			}
		});
	}
	$(function(){
		$.ajax({
			url: ipAddress + 'base/company/getList',
			// url: 'http://localhost:8080/base/company/getList',
			data: {
				order: "ordercode"
			},
			beforeSend: function (request) {
				request.setRequestHeader("Authorization", "Bearer " + getToken());
			},
			method:'GET',
			success:function (data,status,request) {
				console.log(data);
				$("#companyid").append("<option value>请选择分公司</option>");
				for(var i = 0; i < data.length; i ++){
					$("#companyid").append("<option value='" + data[i].companyid + "'>" + data[i].companyname + "</option>");
				}
			},
			error:function (request, msg, obj) {
				var respTxtObj = JSON.parse(request.responseText);
				console.log(respTxtObj);
				console.log(respTxtObj.error);
				console.log(respTxtObj.error_description);
			}
		});
		
		$.ajax({
			 url: ipAddress + 'auth/groups/getList',
			//url: 'http://localhost:8080/auth/groups/getList',
			data:  { },
			beforeSend: function (request) {
				request.setRequestHeader("Authorization", "Bearer " + getToken());
			},
			method:'GET',
			success:function (data,status,request) {
				console.log(data);
				for(var i = 0; i < data.length; i ++){
					$("#groups").append("<option value='" + data[i].id + "'>" + data[i].groupName + "</option>");
				}
			},
			error:function (request, msg, obj) {
				var respTxtObj = JSON.parse(request.responseText);
				console.log(respTxtObj);
				console.log(respTxtObj.error);
				console.log(respTxtObj.error_description);
			}
		});
	});
	
	var vm = new Vue({
		el : "#app",
		data : {
			msg : ""
		},
		//在实例初始化之后，数据观测 (data observer) 和 event/watcher 事件配置之前被调用。
		beforeCreate:function(){
			console.log('beforeCreate');
		},
		/* 在实例创建完成后被立即调用。
		在这一步，实例已完成以下的配置：数据观测 (data observer)，属性和方法的运算，watch/event 事件回调。
		然而，挂载阶段还没开始，$el 属性目前不可见。 */
		created	:function(){
			console.log('created');
		},
		//在挂载开始之前被调用：相关的渲染函数首次被调用
		beforeMount : function(){
			console.log('beforeMount');
	
		},
		//el 被新创建的 vm.$el 替换, 挂在成功	
		mounted : function(){
			console.log('mounted');
		
		},
		//数据更新时调用
		beforeUpdate : function(){
			console.log('beforeUpdate');
				
		},
		//组件 DOM 已经更新, 组件更新完毕 
		updated : function(){
			console.log('updated');
				
		},
		methods: {
			query: function(){
				var data = {
					'username': this.msg,
					'queryEnabled': $('#enabled').val(),
					'like': "%username%",
					'order': "createTime"
				}
				$('#companyid').val() != '' ? data['company.companyid'] = $('#companyid').val() : console.log('companyid is empty string!');
				$('#officeid').val() != '' ? data['office.id'] = $('#officeid').val() : console.log('officeid is empty string!');
				$.ajax({
					type: 'GET',
					url: ipAddress + 'auth/user/getList',
					data: data,
					beforeSend: function (request) {
						request.setRequestHeader("Authorization", "Bearer " + getToken());
					},
					dataType: 'json',
					success: function(data){
						table.fnClearTable();   //将数据清除
						if(data.length > 0){
							table.fnAddData(data,true);  //数据必须是json对象或json对象数组
						}
						console.log(data);
					},
					error: function (request, msg, obj) {
						console.log(request);
						console.log(msg);
						console.log(obj);
						var respTxtObj = JSON.parse(request.responseText);
						console.log(respTxtObj);
						if(respTxtObj.error == "unauthorized" && msg == "error"){
							alert("未经授权!");
							console.log(respTxtObj.error_description);
						}
						if(respTxtObj.error == "unsupported_grant_type" && msg == "error"){
							alert("不支持的授予类型!");
							console.log(respTxtObj.error_description);
						}
						if(respTxtObj.error == "invalid_request" && msg == "error"){
							alert("无效的请求!");
							console.log(respTxtObj.error_description);
						}
						if(respTxtObj.error == "invalid_scope" && msg == "error"){
							alert("无效的作用域!");
							console.log(respTxtObj.error_description + ", valid scope: " + respTxtObj.scope);
						}
						if(respTxtObj.error == "Method Not Allowed" && msg == "error"){
							alert("请求方法错误!");
							console.log(respTxtObj.error_description);
						}
						if(respTxtObj.error == "invalid_token" && msg == "error"){
							alert("令牌无效, 请重新登录!");
							console.log(respTxtObj.error_description);
							location.href = 'login.html';
						}
						if(respTxtObj.error == "access_denied" && msg == "error"){
							alert("权限不足, " + respTxtObj.error_description + "!");
							console.log(respTxtObj.error_description);
							location.href = 'login.html';
						}
					}
				});
			}
		}
	});
	
	var table = $('#list').dataTable({
		"bProcessing" : true, //DataTables载入数据时，是否显示‘进度’提示  
		"bAutoWidth" : true, //是否自适应宽度  
		//stateSave: true,
		"lengthMenu":[10, 50, 100],
		"pageLength": 100,
		"aaSorting": [[ 1, "asc" ]],//默认第几个排序
		
		"aoColumnDefs": [
			{"orderable":false,"aTargets":[0,7]}// 制定列不参与排序
		],
		bDeferRender: true, 
		"aoColumns" : [{  
			"mDataProp" : "username",  
			"sTitle" : "<input type='checkbox'/>" ,
		}, {
			"mDataProp" : "company.companyname",  
			"sTitle" : "分公司",  
			"sDefaultContent" : "无",  
			"sClass" : "center"  ,
		}, {
			"mDataProp" : "office.officename",  
			"sTitle" : "票务室",  
			"sDefaultContent" : "无",  
			"sClass" : "center"  
		}, {  
			"mDataProp" : "username",  
			"sTitle" : "用户名",  
			"sDefaultContent" : "",  
			"sClass" : "center"  
		}, {  
			"mDataProp" : "realname",  
			"sTitle" : "姓名",  
			"sDefaultContent" : "",  
			"sClass" : "center"  ,
		}, {
			"mDataProp" : "groups",  
			"sTitle" : "组",  
			"sDefaultContent" : "",  
			"sClass" : "center"  ,
		}, {  
			"mDataProp" : "enabled",  
			"sTitle" : "是否启用",  
			"sDefaultContent" : "",  
			"sClass" : "center"  ,
		}, {
			"mDataProp" : "createTime",  
			"sTitle" : "创建时间",  
			"sDefaultContent" : "",  
			"sClass" : "center"  
		}, {  
			"mDataProp" : "companyid",  
			"sTitle" : "操作",  
			"sDefaultContent" : "",  
			"sClass" : "center"  
		}],
		"fnRowCallback" : function(nRow, aData, iDisplayIndex) {  
			/* 用来改写用户权限的 */  
			console.log(aData);
			$('td:eq(0)', nRow).html("<input type='checkbox' name='usernames' value='" + aData.username +"'/>");
			
			var groupNames = '';
			$(aData.groups).each(function(i, e){
				groupNames = groupNames + aData.groups[i].groupName + '<br>';
			});
			$('td:eq(5)', nRow).html(groupNames);
			
			var enabledSpan = "<span class='label label-success radius'>已启用</span>";
			if(!aData.enabled){
				enabledSpan = "<span class='label label-defaunt radius'>已停用</span>";
			}
			$('td:eq(6)', nRow).addClass('td-status');
			$('td:eq(6)', nRow).html(enabledSpan);
			
			$('td:eq(8)', nRow).addClass('td-manage');
			var operations;
			var op_disabled = "<a style=\"text-decoration:none\" onClick=\"member_stop(this,'" + aData.username + "')\" href=\"javascript:;\" title=\"停用\"><i class=\"Hui-iconfont\">&#xe631;</i></a>";
			var op_enabled = "<a style=\"text-decoration:none\" onClick=\"member_start(this,'" + aData.username + "')\" href=\"javascript:;\" title=\"启用\"><i class=\"Hui-iconfont\">&#xe6e1;</i></a>";
			var op_edit = "<a title=\"编辑\" href=\"javascript:;\" onclick=\"member_edit('编辑','authinfo-user-update.html','" + aData.username + "','','510')\" class=\"ml-5\" style=\"text-decoration:none\"><i class=\"Hui-iconfont\">&#xe6df;</i></a>";
			var op_changepwd = "<a style=\"text-decoration:none\" class=\"ml-5\" onClick=\"change_password('修改密码','authinfo-change-password.html','"+ aData.username +"','600','270')\" href=\"javascript:;\" title=\"修改密码\"><i class=\"Hui-iconfont\">&#xe63f;</i></a>";
			var op_del = "<a title=\"删除\" href=\"javascript:;\" onclick=\"member_del(this,'" + aData.username + "')\" class=\"ml-5\" style=\"text-decoration:none\"><i class=\"Hui-iconfont\">&#xe6e2;</i></a>";
			if(hasRole("ADMIN")) {
				if(aData.enabled){
					operations = op_disabled;
				} else {
					operations = op_enabled;
				}
				operations = operations + op_edit + op_changepwd + op_del;
			}
			$('td:eq(8)', nRow).html(operations);
			return nRow;  
		},
		"sAjaxSource" : ipAddress + "auth/user/getTableList",
		// "sAjaxSource" : "http://localhost:8080/auth/user/getTableList",
		//服务器端，数据回调处理  
		"fnServerData" : function(sSource, aDataSet, fnCallback) {  
			aDataSet = {"username": vm.msg, "queryEnabled": $('#enabled').val(), "like": "%username%", "order": "createTime"};
			console.log(aDataSet);
			$.ajax({  
				"dataType" : 'json',  
				"type" : "GET",  
				"url" : sSource,  
				"data" : aDataSet,  
				"beforeSend": function (request) {
					request.setRequestHeader("Authorization", "Bearer " + getToken());
				},
				"success" : fnCallback ,
				"error" : function (request, msg, obj) {
					console.log(request);
					console.log(msg);
					console.log(obj);
					var respTxtObj = JSON.parse(request.responseText);
					console.log(respTxtObj);
					if(respTxtObj.error == "unauthorized" && msg == "error"){
						alert("未经授权!");
						console.log(respTxtObj.error_description);
					}
					if(respTxtObj.error == "unsupported_grant_type" && msg == "error"){
						alert("不支持的授予类型!");
						console.log(respTxtObj.error_description);
					}
					if(respTxtObj.error == "invalid_request" && msg == "error"){
						alert("无效的请求!");
						console.log(respTxtObj.error_description);
					}
					if(respTxtObj.error == "invalid_scope" && msg == "error"){
						alert("无效的作用域!");
						console.log(respTxtObj.error_description + ", valid scope: " + respTxtObj.scope);
					}
					if(respTxtObj.error == "Method Not Allowed" && msg == "error"){
						alert("请求方法错误!");
						console.log(respTxtObj.error_description);
					}
					if(respTxtObj.error == "invalid_token" && msg == "error"){
						alert("令牌无效, 请重新登录!");
						console.log(respTxtObj.error_description);
						location.href = 'login.html';
					}
					if(respTxtObj.error == "access_denied" && msg == "error"){
						alert("权限不足, " + respTxtObj.error_description + "!");
						console.log(respTxtObj.error_description);
						location.href = 'login.html';
					}
				}
			});  
		}  
	});
	
</script>
</body>
</html>